﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Helper;
using System.Data;
using Framework;

namespace DomMgmtSystem.View
{
    public partial class InCheckListHS : System.Web.UI.Page
    {
        string sqlWhere = " ";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                GetPage();
        }
        /*分页*/
        public void GetPage()
        {
            /*求出总记录数*/
            int c = DataAccess.TB_CHECK_HS.GetCount(sqlWhere);
            /*判断当前页面是否为0*/

            /*显示设置*/
            AspNetPager1.AlwaysShow = true;
            /*每页显示数量*/
            AspNetPager1.PageSize = 10;
            /*记录总页数*/
            AspNetPager1.RecordCount = c;
            /*绑定数据源*/
            ListDataBind();
            if (c > 0)
            {
                //dv1.Visible = false;
                Repeater1.Visible = true;
            }
            else
            {
                //dv1.Visible = true;
                Repeater1.Visible = false;
            }
        }

        /*分页*/
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            ListDataBind();
        }
        /*分页*/
        public void ListDataBind()
        {
            Repeater1.DataSource = DataAccess.TB_CHECK_HS.GetList(sqlWhere, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize);
            Repeater1.DataBind();
        }

        void SetSqlWhere()
        {
            sqlWhere = " 1=1 ";

            if (txtSQDH.Text.Trim() != "")
                sqlWhere += " and SQDH like '%" + txtSQDH.Text.Trim() + "%'";

            if (txtCSQMC.Text.Trim() != "")
                sqlWhere += string.Format(" and CSQMC like'%{0}%'", txtCSQMC.Text.Trim());

            if (txtStart.Text.Trim().IsNotEmpty())
                sqlWhere += " and JC_DATE >=to_date('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')".FormatWith(txtStart.Text.Trim());

            if (txtEnd.Text.Trim().IsNotEmpty())
                sqlWhere += " and JC_DATE <=to_date('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')".FormatWith(txtEnd.Text.Trim());

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                SetSqlWhere();
                ListDataBind();
            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("检查列表查询异常:{0}{1}", ex.Message, ex.StackTrace);
                this.Alert("系统异常！");
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Del")
                {
                    decimal nID = Convert.ToDecimal(e.CommandArgument.ToString());
                    DataAccess.TB_CHECK_HS.Delete(nID);
                    ListDataBind();
                    //this.Alert("删除成功！");
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("检查列表Repeater1_ItemCommand异常:{0}{1}", ex.Message, ex.StackTrace);
                this.Alert("系统异常！");
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            try
            {
                SetSqlWhere();
                DataSet dt = DataAccess.TB_CHECK_HS.GetListForExport(sqlWhere);
                if (dt.Tables[0].Columns.Contains("NID"))
                    dt.Tables[0].Columns.Remove("NID");
                if (dt.Tables[0].Columns.Contains("JCSFTG"))
                    dt.Tables[0].Columns.Remove("JCSFTG");
                if (dt.Tables[0].Columns.Contains("RKZT"))
                    dt.Tables[0].Columns.Remove("RKZT");
                foreach (DataColumn item in dt.Tables[0].Columns)
                {
                    if (item.ColumnName == "SQDH")
                        item.ColumnName = "摄区编码";
                    else if (item.ColumnName == "CSQMC")
                        item.ColumnName = "摄区名称";
                    else if (item.ColumnName == "JC_DATE")
                        item.ColumnName = "检查日期";
                }
                NPOIHelper.ExportByWeb(dt.Tables[0], "入库检查", "Data{0}.xls".FormatWith(DateTime.Now.ToString("yyyyMMddHHssmm")));
            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("导出检查异常:{0}{1}", ex.Message, ex.StackTrace);
                this.Alert("系统异常！");
            }

        }
    }
}